package utils

import (
	"fmt"
	"github.com/shopspring/decimal"
	"strconv"
	"strings"
)

// uint to string
func UintToStr(unitvalue uint) string {
	return fmt.Sprintf("%d", unitvalue) // todo 只能用这么挫的方法吗
}

// int to string
func IntToStr(intvalue int) string {
	return strconv.Itoa(intvalue)
}

// string to int
func StrToInt(str string) int {
	intValue, err := strconv.Atoi(str)
	if err != nil {
		intValue = int(StrToFloat(str))
	}
	return intValue
}

// string 去千分符
func TrimQff(str string) string {
	return strings.Replace(str, ",", "", -1)
}

func StrToFloat(str string) float64 {
	str = Trim(str)
	str = TrimQff(str)
	if str == "NaN" {
		str = "0"
	}
	intValue, err := strconv.ParseFloat(str, 64)
	if err != nil {
		intValue = 0
	}
	d := decimal.NewFromFloat(intValue)
	//fmt.Println(n.String())
	f, _ := d.Round(2).Float64()
	return f
}

func StrToFloat2(str string, n int32) float64 {
	intValue, err := strconv.ParseFloat(str, 64)
	if err != nil {
		intValue = 0
	}
	d := decimal.NewFromFloat(intValue)
	//fmt.Println(n.String())
	f, _ := d.Round(n).Float64()
	return f
}

func StrToUint(str string) uint {
	intValue, _ := strconv.Atoi(str)
	return uint(intValue)
}

// WeekdayConverter 星期几转换器
// weekday: 英文星期名称（如"Thursday"）
// lang: "en"返回英文，"cn"返回中文
func WeekdayConverter(weekday string, lang string) string {
	weekday = strings.ToLower(weekday)
	lang = strings.ToLower(lang)

	weekMap := map[string]map[string]string{
		"monday":    {"en": "Monday", "cn": "周一"},
		"tuesday":   {"en": "Tuesday", "cn": "周二"},
		"wednesday": {"en": "Wednesday", "cn": "周三"},
		"thursday":  {"en": "Thursday", "cn": "周四"},
		"friday":    {"en": "Friday", "cn": "周五"},
		"saturday":  {"en": "Saturday", "cn": "周六"},
		"sunday":    {"en": "Sunday", "cn": "周日"},
	}

	if day, ok := weekMap[weekday]; ok {
		if converted, exists := day[lang]; exists {
			return converted
		}
	}

	// 默认返回英文
	if lang == "en" {
		return weekday
	}
	return weekday // 或者可以根据需要返回"未知"
}
